﻿using Ross.EntityFramework;
using Ross.Service;
using Ross.Service.Dto;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Results;

namespace Ross.Traning.Controllers
{
    [SimpleAuthenticationFilter]
    public class CourseController : BaseApiController
    {
        [HttpPost]
        public JsonResult<ResultDto<bool>> Insert([FromBody] Service.Dto.Course vm)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (CourseService serv = new CourseService(dbContextStr))
            {
                result = serv.Create(vm);
            }
            return Json(result);
        }
        /// <summary>
        /// 上传课程图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<string> UploadImage()
        {
            ResultDto<string> result = new ResultDto<string>();
            try
            {
                string absPath = HttpContext.Current.Server.MapPath(uploadImgPath);
                if (!Directory.Exists(absPath))
                {
                    Directory.CreateDirectory(absPath);
                }
                HttpRequest request = System.Web.HttpContext.Current.Request;
                HttpFileCollection fileCollection = request.Files;
                if (fileCollection.Count > 0)
                {
                    HttpPostedFile httpPostedFile = fileCollection[0];
                    string fileExtension = Path.GetExtension(httpPostedFile.FileName);// 文件扩展名
                    string OKExtension = ConfigurationManager.AppSettings["UploadImageType"];
                    if (OKExtension.IndexOf(fileExtension.Substring(1)) < 0)
                    {
                        result.status = 500;
                        result.datas = null;
                        result.message = "上传失败！文件类型错误！";
                    }
                    else
                    {
                        string fileName = Guid.NewGuid().ToString() + fileExtension;// 文件名称
                        string filePath = absPath + httpPostedFile.FileName;// 上传路径
                        
                        string sfile = absPath + fileName + fileExtension;
                        while (File.Exists(filePath))
                        {
                            fileName = Guid.NewGuid().ToString() + fileExtension;
                            filePath = absPath + fileName;
                        }
                        httpPostedFile.SaveAs(filePath);
                        result.status = 200;
                        result.datas = uploadImgPath + httpPostedFile.FileName;
                        result.message = "上传成功！";
                        //Course cs = new Course();
                        //cs.ID = CourseID;
                        //cs.ImgURL = ConfigurationManager.AppSettings["UploadImagePath"] + httpPostedFile.FileName;
                        //using (CourseService serv = new CourseService(dbContextStr))
                        //{
                        //    serv.UpdateByID(cs);
                        //}
                        //result = "上传成功";
                    }

                }
            }
            catch (Exception ex)
            {
                result.status = 500;
                result.datas = null;
                result.message = "上传失败"+ex.Message;
            }
            return result;
        }
        [HttpGet]
        public JsonResult<ResultDto<List<Service.Dto.Course>>> GetPageList(string keywords = "", string classid = "", string cType = "",long userId=0, int pageindex = 1, int pagesize = 10)
        {
            ResultDto<List<Service.Dto.Course>> result = new ResultDto<List<Service.Dto.Course>>();
            using (CourseService serv = new CourseService(dbContextStr))
            {
                result = serv.GetPageList(pageindex, pagesize, keywords, classid, cType, userId);
            }
            return Json(result);
        }
        [HttpGet]
        public JsonResult<ResultDto<List<Course>>> GetSignCourse(long userId = 0, int pageindex = 1, int pagesize = 10)
        {
            ResultDto<List<Course>> result = new ResultDto<List<Course>>();
            using (CourseService serv = new CourseService(dbContextStr))
            {
                result = serv.GetSignCourse(pageindex, pagesize, userId);
            }
            return Json(result);
        }
        [HttpGet]
        public JsonResult<ResultDto<Service.Dto.Course>> GetById(long id)
        {
            ResultDto<Service.Dto.Course> result = new ResultDto<Service.Dto.Course>();
            using (CourseService serv = new CourseService(dbContextStr))
            {
                result = serv.GetById(id);
            }
            return Json(result);
        }
        [HttpPost]
        public JsonResult<ResultDto<bool>> Delete(long id)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (CourseService serv = new CourseService(dbContextStr))
            {
                result = serv.Delete(id);
            }
            return Json(result);
        }
        /// <summary>
        /// 更新课程接口
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult<ResultDto<bool>> Update([FromBody] Course vm)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (CourseService serv = new CourseService(dbContextStr))
            {
                result = serv.Update(vm);
            }
            return Json(result);
        }
        [HttpPost]
        public JsonResult<ResultDto<bool>> Score([FromBody] Course cc)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (CourseService serv = new CourseService(dbContextStr))
            {
                result = serv.Score(cc);
            }
            return Json(result);
        }
        [HttpGet]
        public JsonResult<ResultDto<List<Course>>> GetLists()
        {
            ResultDto<List<Course>> result = new ResultDto<List<Course>>();
            using (CourseService serv = new CourseService(dbContextStr))
            {
                result = serv.GetAllList();
            }
            return Json(result);
        }
    }
}
